package com.mlz.mlzmall.ui.adapter.base;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.mlz.mlzmall.ui.activity.act.main.MyApplication;
import com.mlz.mlzmall.ui.utils.util.BitmapCache;

import java.util.List;
/**
 * 功能描述：所有Adapter都可以直接继承ShopMallAdapter
 * Created by benny on 2016/5/7.
 * ProjectName：mlzMall.
 */
public abstract class ShopMallAdapter<T> extends BaseAdapter {

	protected Context context;
	protected List<T> data;
	protected LayoutInflater myInflater;
	protected int layoutId;
	protected Activity activity;
	private ImageLoader imageLoader;
	public ShopMallAdapter() {
	}

	public ShopMallAdapter(Activity activity, List<T> data, int layoutId) {
		this.data = data;
		this.layoutId = layoutId;
		this.activity = activity;
		this.context = activity;
		myInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public T getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MallHolder holder = MallHolder.getViewHolder(context, convertView, parent, this.layoutId,
				position);
		convert(holder, getItem(position));
		return holder.getConvertView();
	}

	public ImageLoader getImageLoader() {
		if (this.imageLoader == null) {
			MyApplication myApplication = (MyApplication) activity.getApplication();
			RequestQueue rq = myApplication.getRequestQueue();
			this.imageLoader = new ImageLoader(rq, new BitmapCache());
		}
		return this.imageLoader;
	}

	public abstract void convert(MallHolder holder, T t);

}
